package com.project.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.EmployeeBean;
import com.project.bean.InboxBean;
import com.project.bean.MailBean;
import com.project.mapper.IEmployeeMapper;
import com.project.mapper.IInboxMapper;
import com.project.mapper.IMailMapper;
import com.project.service.IInboxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;

@Service
@Transactional
public class InboxServiceImpl implements IInboxService {
    @Autowired
    private IEmployeeMapper emMapper;
    @Autowired
    private IMailMapper mailMapper;
    @Autowired
    private IInboxMapper inboxMapper;

    @Override
    public IPage<InboxBean> cutByItem(Integer pageNO, Integer receiverId, String subject, LocalDate startDate, LocalDate endDate) {
        return inboxMapper.cutByItem(new Page<>(pageNO, 3),receiverId,subject,startDate,endDate );
    }

    @Override
    public MailBean findById(Integer inboxId, Integer mailId) {
        MailBean mail = mailMapper.selectById(mailId);
        EmployeeBean em = emMapper.selectById(mail.getSenderId());
        mail.setSender(em);
        mailMapper.updateById(mail);
        InboxBean inboxMail = inboxMapper.selectById(inboxId);
        inboxMail.setReadingStatus("已阅读");
        inboxMapper.updateById(inboxMail);
        return mail;
    }

    @Override
    public void del(Integer inboxId) {
        inboxMapper.deleteById(inboxId);
    }
}
